﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rem.Objects;

namespace Rem.Explorer
{
    class RemTreeNode : TreeNode
    {
        public MenuNodeType NodeType { get; set; }
        public string Path { get; set; }
        public BaseObjectTypes BaseType { get; set; }
        public object Query { get; set; }
        public Form Form { get; set; }
        public ContextMenuStrip ContextMenu { get; set; }
        public long ID { get; set; }
        public bool UserCreatedNode { get; set; }

        public RemTreeNode()
            : base()
        {
            this.ContextMenu = new ContextMenuStrip();
        }
        public RemTreeNode(string text, int imageIndex, int selectedImageIndex)
            : base(text, imageIndex, selectedImageIndex)
        {
            this.ContextMenu = new ContextMenuStrip();
        }
        public RemTreeNode(string text, int imageIndex, int selectedImageIndex, TreeNode[] children)
            : base(text, imageIndex, selectedImageIndex, children)
        {
            this.ContextMenu = new ContextMenuStrip();
        }
        public RemTreeNode(string text, int imageIndex, int selectedImageIndex,
           string path, MenuNodeType nodeType, bool userCreatedNode)
            : base(text, imageIndex, selectedImageIndex)
        {
            this.Path = path;
            this.NodeType = nodeType;
            this.UserCreatedNode = userCreatedNode;
            
        }

        public void ShowContextMenu(int x, int y)
        {

            if (this.ContextMenu.Items == null || this.ContextMenu.Items.Count == 0)
            {
                switch (this.NodeType)
                {
                    case MenuNodeType.ExistingWindow:
                        this.ContextMenu.Items.Add(new RemMenuItem("显示", null, this.OnShowForm, this.BaseType));
                        this.ContextMenu.Items.Add(new RemMenuItem("隐藏", null, this.OnHideForm, this.BaseType));
                        this.ContextMenu.Items.Add(new RemMenuItem("关闭", null, this.OnCloseForm, this.BaseType));
                        this.ContextMenu.Items.Add(new RemMenuItem("离开", null, this.OnDisjoinForm, this.BaseType));
                        break;
                    case MenuNodeType.QueryEntry:
                        break;
                    case MenuNodeType.LaunchApp:
                        break;
                    case MenuNodeType.Link:
                        break;
                    case MenuNodeType.RemApp:
                        break;
                    case MenuNodeType.BasicInfo:
                        break;
                    case MenuNodeType.Employees:
                        this.ContextMenu.Items.Add(new RemMenuItem("新建员工", null, FormUtilities.NewObject, this.BaseType));
                        this.ContextMenu.Items.Add(new RemMenuItem("所有员工", null, FormUtilities.NewObject, BaseObjectTypes.EmployeeQuery));
                        this.ContextMenu.Items.Add(new RemMenuItem("关闭所有", null, this.OnCloseAll, this.BaseType));
                        break;
                    case MenuNodeType.Contracts:
                        
                        this.ContextMenu.Items.Add(new RemMenuItem("新建合同", null, FormUtilities.NewObject, this.BaseType));
                        this.ContextMenu.Items.Add(new RemMenuItem("所有合同", null, FormUtilities.NewObject, BaseObjectTypes.ContractQuery));
                        this.ContextMenu.Items.Add(new RemMenuItem("关闭所有", null, this.OnCloseAll, this.BaseType));
                        break;
                    case MenuNodeType.Customers:
                        this.ContextMenu.Items.Add(new RemMenuItem("新建客户", null, FormUtilities.NewObject, this.BaseType));
                        this.ContextMenu.Items.Add(new RemMenuItem("所有客户", null, FormUtilities.NewObject, BaseObjectTypes.CustomerQuery));
                        this.ContextMenu.Items.Add(new RemMenuItem("关闭所有", null, this.OnCloseAll, this.BaseType));
                        break;
                    case MenuNodeType.Other:
                        break;
                    case MenuNodeType.Tools:
                        break;
                    case MenuNodeType.Reminds:
                        this.ContextMenu.Items.Add(new RemMenuItem("新建提醒", null, FormUtilities.NewObject, this.BaseType));
                        this.ContextMenu.Items.Add(new RemMenuItem("所有提醒", null, FormUtilities.NewObject, BaseObjectTypes.ReminderQuery));
                        this.ContextMenu.Items.Add(new RemMenuItem("关闭所有", null, this.OnCloseAll, this.BaseType));
                        break;
                    default:
                        break;
                }
            }
            if (this.ContextMenu != null)
                ContextMenu.Show(this.TreeView, new System.Drawing.Point(x, y));
            //todo
        }

        public void OnCloseForm(Object sender, EventArgs e)
        {
            if (this.Form != null)
            {
                this.Form.Close();
                this.Form = null;
            }
            else if (this.UserCreatedNode)
            {
                this.Remove();
            }
        }

        public void OnShowForm(Object sender, EventArgs e)
        {
            if (this.Form != null)
            {
                if (this.Form.WindowState == FormWindowState.Minimized)
                {
                    this.Form.WindowState = FormWindowState.Normal;
                }
                Form.Show();
            }
        }

        public void OnDisjoinForm(Object sender, EventArgs e)
        {
            this.Form.MdiParent = null;
        }
        public void OnHideForm(Object sender, EventArgs e)
        {
            Form.Hide();
        }

        public void OnCloseAll(Object sender, EventArgs e)
        {
            TreeNodeCollection nodes = this.Nodes;
            if (sender.GetType() != typeof(RemTreeNode))
            {
                nodes = this.Nodes;
            }
            List<TreeNode> tempNodes = new List<TreeNode>(nodes.Count);
            foreach (TreeNode node in nodes)
            {
                tempNodes.Add(node);
            }
            foreach (var node in tempNodes)
            {
                if ((node as RemTreeNode).NodeType == MenuNodeType.ExistingWindow)
                {
                    (node as RemTreeNode).OnCloseForm(sender, e);
                }
                else if ((node as RemTreeNode).NodeType == MenuNodeType.QueryEntry &&
                    (node as RemTreeNode).Form != null)
                {
                    (node as RemTreeNode).OnCloseForm(sender, e);
                }
                else
                {
                    OnCloseAll((node as RemTreeNode), e);

                }
            }
        }

        public void OnCloneForm()
        {
            if (this.Form != null)
            {
                this.Form.Close();
                this.Form = null;
            }
            else if (this.UserCreatedNode)
            {
                this.Remove();
            }
        }

        public void OnOpenQueryEntry(Object sender, EventArgs e)
        {
            FormUtilities.OpenObjectForm(this.BaseType, null, false);
            this.Form.Text = this.Text;
            this.Form.Show();
        }
    }
}
